//
//  WATransactionDetailController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/17.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WATransactionDetailController.h"
#import "WAGoodListModel.h"
#import "WNSingleImageBrowser.h"

@interface WATransactionDetailController ()
//订单状态
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *orderStutusActiveView;
@property (weak, nonatomic) IBOutlet UILabel *transactionStatusLB;
@property (weak, nonatomic) IBOutlet UILabel *priceLB;
@property (weak, nonatomic) IBOutlet UILabel *shouxufeiLB;
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLB;
@property (weak, nonatomic) IBOutlet UILabel *exchangeCountLB;
@property (weak, nonatomic) IBOutlet UILabel *transNumLB;
@property (weak, nonatomic) IBOutlet UILabel *orderTimeLB;

//卖家信息
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *sellActiveView;
@property (weak, nonatomic) IBOutlet UILabel *sellNameLB;
@property (weak, nonatomic) IBOutlet UILabel *sellPhoneLB;
@property (weak, nonatomic) IBOutlet UILabel *bankNameLB;
@property (weak, nonatomic) IBOutlet UILabel *bankNumLB;

//买家信息
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *buyActiveView;
@property (weak, nonatomic) IBOutlet UILabel *buyNameLB;
@property (weak, nonatomic) IBOutlet UILabel *bugPhoneLB;

//其他
@property (weak, nonatomic) IBOutlet UIButton *uploadPayImageBtn;
@property (weak, nonatomic) IBOutlet UIImageView *payImageView;//支付截图
@property (weak, nonatomic) IBOutlet UILabel *statusLB;
@property (weak, nonatomic) IBOutlet UIButton *appealButton;//申诉
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *appealBtnRightCons;//申诉约束
@property (weak, nonatomic) IBOutlet UIButton *alreadyPayButton;


@property (strong, nonatomic) WAGoodListModel *orderModel;
@property (strong, nonatomic) WNUserInfo *buyerModel;//买家信息
@property (strong, nonatomic) WNUserInfo *sellerModel;//卖家信息

@property (strong, nonatomic) WAGoodListModel *paraModel;//前一个页面的传参
//判断当前用户是否是买家
@property (nonatomic, assign) NSInteger type;
@end

@implementation WATransactionDetailController

- (instancetype)initWithModel:(WAGoodListModel *)model type:(NSInteger)type
{
    self = [super init];
    if (self) {
        self.paraModel = model;
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.payImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction)];
    [self.payImageView addGestureRecognizer:tap];
    [self requesrtData];
}

- (void)requesrtData{
    self.payImageView.hidden = YES;
    self.uploadPayImageBtn.hidden = YES;
    self.alreadyPayButton.hidden = YES;
    [self getTransactionInfo];
    [self getBuyerInfo];
    [self getSellerINfo];
}

//获取订单信息
- (void)getTransactionInfo{
    [self.orderStutusActiveView start];
    [WNBusinessRequest getTransactionInfo:self.paraModel.ID Succ:^(id  _Nonnull responseObject) {
        self.orderModel = [WAGoodListModel mj_objectWithKeyValues:responseObject];
        [self updateOrderModel];
        [self.orderStutusActiveView stop];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [self.orderStutusActiveView stop];
        [WNTool show:mes];
    }];
}

//获取买家信息
- (void)getBuyerInfo{
    [self.buyActiveView start];
    NSInteger ID;
    if (self.type == 1 || self.type == 2) {
        ID = self.paraModel.dealuserid;
    }else{
        ID = self.paraModel.userid;
    }
    [WNBusinessRequest getUserInfoWithID:[@(ID) stringValue] Succ:^(id  _Nonnull responseObject) {
        self.buyerModel = [WNUserInfo mj_objectWithKeyValues:responseObject];
        [self updateBuyerViews];
        [self.buyActiveView stop];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [self.buyActiveView stop];
        [WNTool show:mes];
    }];
}

//获取卖家信息
- (void)getSellerINfo{
    [self.sellActiveView start];
    NSInteger ID;
    if (self.type == 1 || self.type == 2) {
        ID = self.paraModel.userid;
    }else{
        ID = self.paraModel.dealuserid;
    }
    [WNBusinessRequest getUserInfoWithID:[@(ID) stringValue] Succ:^(id  _Nonnull responseObject) {
        self.sellerModel = [WNUserInfo mj_objectWithKeyValues:responseObject];
        [self updateSellerViews];
        [self.sellActiveView stop];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [self.sellActiveView stop];
        [WNTool show:mes];
    }];
}

//卖家模块
- (void)updateSellerViews{
    self.sellNameLB.text = self.sellerModel.name;
    self.sellPhoneLB.text = self.sellerModel.phone;
    self.bankNameLB.text = self.sellerModel.bankname;
    self.bankNumLB.text = self.sellerModel.cardnum;
}

//买家模块
- (void)updateBuyerViews{
    self.buyNameLB.text = self.buyerModel.name;
    self.bugPhoneLB.text = self.buyerModel.phone;
}

//订单模块
- (void)updateOrderModel{
    self.payImageView.hidden = YES;
    self.uploadPayImageBtn.hidden = YES;
    self.alreadyPayButton.hidden = YES;
    //订单模块
    
    if (self.orderModel.dealtime.length == 14) {
        self.orderTimeLB.text = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                                 [self.orderModel.dealtime substringWithRange:NSMakeRange(0, 4)],
                                 [self.orderModel.dealtime substringWithRange:NSMakeRange(4, 2)],
                                 [self.orderModel.dealtime substringWithRange:NSMakeRange(6, 2)],
                                 [self.orderModel.dealtime substringWithRange:NSMakeRange(8, 2)],
                                 [self.orderModel.dealtime substringWithRange:NSMakeRange(10, 2)],
                                 [self.orderModel.dealtime substringWithRange:NSMakeRange(12, 2)]];
    }
    
    self.transNumLB.text = [NSString stringWithFormat:@"%zd",self.orderModel.ID];
//    self.priceLB.text = [NSString stringWithFormat:@"￥%.2f",self.orderModel.price];
    self.priceLB.text = [NSString stringWithFormat:@"￥%@",[NSString baoliuliangwei:[@(self.orderModel.price) stringValue]]];
    self.transNumLB.text = [@(self.orderModel.ID) stringValue];
    self.totalPriceLB.text = [NSString stringWithFormat:@"￥%.1f",self.orderModel.totalfee];
    self.exchangeCountLB.text = [NSString stringWithFormat:@"%zd枚",self.orderModel.number];
    self.shouxufeiLB.text = [NSString stringWithFormat:@"%.1f枚",self.orderModel.yongjin];
    
    
    if ([self.orderModel.imageurl hasPrefix:@"http"]) {
        [self.payImageView sd_setImageWithURL:[NSURL URLWithString:self.orderModel.imageurl]];
    }else{
        if (self.orderModel.imageurl.length > 0) {
            self.payImageView.image = [UIImage stringToImage:self.orderModel.imageurl];
        }
    }
    
    //我的买单交换中
    if (self.paraModel.status == 2) {
        self.transactionStatusLB.text = @"待确认";
    }else{
        self.transactionStatusLB.text = @"待支付";
    }
    //我的买单交换中
    if (self.paraModel.transactiontype == 0) {
        if ((self.userID_Int == self.orderModel.userid)
            && self.orderModel.imageurl.length == 0) {
                self.payImageView.hidden = YES;
                self.payImageView.userInteractionEnabled = NO;
                self.uploadPayImageBtn.hidden = NO;
                self.alreadyPayButton.hidden = NO;
                [self.uploadPayImageBtn setTitle:@"上传支付截图" forState:0];
        }
        else if ((self.userID_Int == self.orderModel.dealuserid)
                 && self.orderModel.imageurl.length > 0){
                     [self.alreadyPayButton setTitle:@"确认收款" forState:0];
                     self.payImageView.userInteractionEnabled = YES;
                     self.payImageView.hidden = NO;
                     self.alreadyPayButton.hidden = NO;
        }else{
            self.payImageView.userInteractionEnabled = YES;
            self.payImageView.hidden = NO;
        }
    }
    
    //我的卖单交换中
    else{
        if (!(self.orderModel.dealuserid == 0)
            && (self.userID_Int == self.orderModel.dealuserid)
            && self.orderModel.imageurl.length == 0) {
                self.payImageView.hidden = YES;
                self.payImageView.userInteractionEnabled = NO;
                self.uploadPayImageBtn.hidden = NO;
                self.alreadyPayButton.hidden = NO;
                [self.uploadPayImageBtn setTitle:@"上传支付截图" forState:0];
            
        }else{
            if (!(self.userID_Int == self.orderModel.dealuserid)
                && self.orderModel.imageurl.length > 0) {
                    [self.alreadyPayButton setTitle:@"确认收款" forState:0];
                    self.payImageView.userInteractionEnabled = YES;
                    self.payImageView.hidden = NO;
                    self.alreadyPayButton.hidden = NO;
            }else{
                self.payImageView.userInteractionEnabled = YES;
                self.payImageView.hidden = NO;
            }
        }
    }
    
    //我的买单交换中
//    if (self.paraModel.status == 2) {
//        if (self.orderModel.imageurl.length == 0) {
//            self.transactionStatusLB.text = @"待支付";1
//            self.payImageView.hidden = YES;
//            self.payImageView.userInteractionEnabled = NO;
//            self.uploadPayImageBtn.hidden = NO;
//            self.alreadyPayButton.hidden = NO;
//            [self.uploadPayImageBtn setTitle:@"上传支付截图" forState:0];
//        }else{
//            self.transactionStatusLB.text = @"待确认";
//            self.payImageView.userInteractionEnabled = YES;
//            self.payImageView.hidden = NO;
//            self.uploadPayImageBtn.hidden = YES;
//            self.alreadyPayButton.hidden = YES;
//        }
//    }
//    //我的卖单交换中
//    else{
//        self.uploadPayImageBtn.hidden = YES;
//        if (self.orderModel.imageurl.length == 0) {
//            self.transactionStatusLB.text = @"待支付";
//            self.payImageView.hidden = YES;
//            self.payImageView.userInteractionEnabled = NO;
//            self.alreadyPayButton.hidden = YES;
//        }else{
//            self.transactionStatusLB.text = @"待确认";
//            self.payImageView.userInteractionEnabled = YES;
//            self.payImageView.hidden = NO;
//            self.alreadyPayButton.hidden = NO;
//            [self.alreadyPayButton setTitle:@"确认收款" forState:0];
//        }
//    }
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//上传支付截图
- (IBAction)uploadPayImageAction:(id)sender {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    imagePickerVc.allowPickingVideo = NO;
    @weakify(self)
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        UIImage *image = photos.firstObject;
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self)
            self.payImageView.userInteractionEnabled = NO;
            self.payImageView.image = image;
            self.payImageView.hidden = NO;
            self.uploadPayImageBtn.hidden = NO;
            [self.uploadPayImageBtn setTitle:@"" forState:0];
        });
    }];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

//查看图片
- (void)tapImageAction{
    CGRect initialFrame = [self.payImageView.superview convertRect:self.payImageView.frame toView:self.view];
    [[WNSingleImageBrowser Instance] showSingImage:self.payImageView.image initFrame:initialFrame hiddenDownloadBtn:YES hiddenBlock:^{
//        self.formVC.gk_statusBarHidden = NO;
    }];
}

//申诉
- (IBAction)appealBtnAction:(id)sender {
}
//我已支付   确认收款
- (IBAction)alreadPayAction:(id)sender {
    //我的买单交换中
    if ([self.uploadPayImageBtn.titleLabel.text isEqualToString:@"上传支付截图"] && !self.uploadPayImageBtn.hidden) {
        if (!self.payImageView.image) {
            [WNTool show:@"未上传支付截图"];
            return;
        }
        [WNTool showLoadingMessage:@"正在上传"];
        [self uploadPayImage];
    }
    //我的卖单交换中
    else{
        [WNTool showLoadingMessage:@"正在确认"];
        [WNBusinessRequest updateTransactioninfoWithType:3 ID:self.orderModel.ID status:2 image:nil Succ:^(id  _Nonnull responseObject) {
            [WNTool show:@"确认成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self backAction:nil];
            });
        } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
            [WNTool show:mes];
        }];
    }
}

- (void)uploadPayImage{
    NSString *imageName = [NSString stringWithFormat:@"%@-%@",self.userID_String,[NSDate getNowTimeTimestamp]];
    [WAQiniuMananger uploadImage:self.payImageView.image key:imageName progressHandler:^(NSString *key, float percent) {
        NSLog(@"--percent-%.1f",percent);
    } complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        if ([key hasPrefix:@"http"]) {
            [self updateUserInfoWithImageUrl:key];
        }else{
            [WNTool show:key];
        }
    }];
    
    
}

- (void)updateUserInfoWithImageUrl:(NSString *)imageURL{
    [WNBusinessRequest updateTransactioninfoWithType:2 ID:self.orderModel.ID status:1 image:imageURL Succ:^(id  _Nonnull responseObject) {
        [WNTool show:@"上传截图成功"];
        [self getTransactionInfo];
        self.alreadyPayButton.hidden = YES;
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}
@end



